<?php
/**
 * 房源模型
 */

namespace app\badmin\model;


use app\badmin\model\BusinessArea;
use app\badmin\model\HouseVillage;
use app\api\model\Villa;
use app\common\lib\RespCode;
use app\common\model\BaseModel;
use custom\Err;
use PDOStatement;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\Model;

class House extends BaseModel {

    /**
     * 列表字段
     * @var array
     */
    public static $h_column = ['h.is_shelf,h.title,h.id,h.build_area,h.floor,h.bedroom,h.area,h.cover,h.important,h.source_type,ha.source_url'];
    public static $ha_column = ['ha.type,ha.auction_status,ha.start_time,ha.end_time,IF(ha.type=4,ha.pay_selloff,ha.initial_price) as initial_price,ha.discount'];
    public static $hv_column = ['hv.name as hv_name'];
    public static $ht_column = ['ht.name as ht_name'];

    const CONFIG_TYPE1 = 1;//价格
    const CONFIG_TYPE2 = 2;//面积
    const CONFIG_TYPE3 = 3;//折扣
    const CONFIG_TYPE4 = 4;//楼龄
    const CONFIG_TYPE5 = 5;//楼层

    //捡漏
    static $ha_type = [2, 3, 4, 5, 6];
    static $ha_astatus = [1, 2];

    static $whereList = [
        'a_type'          => [
            ['key' => 1, 'value' => '一拍'],
            ['key' => 2, 'value' => '二拍'],
            ['key' => 4, 'value' => '变卖'],
            ['key' => 5, 'value' => '重新拍卖'],
        ],
        'a_status'        => [
            ['key' => 1, 'value' => '待开始'],
            ['key' => 2, 'value' => '进行中'],
            ['key' => 3, 'value' => '已成交'],
            ['key' => 4, 'value' => '已流拍'],
            ['key' => 5, 'value' => '已撤回'],
            ['key' => 6, 'value' => '已终止'],
            ['key' => 7, 'value' => '已暂缓'],
        ],
        'other'           => [
            ['key' => 'hot', 'value' => '重点房源'],
            ['key' => 'today', 'value' => '即将开拍'],
            ['key' => 'pick_up', 'value' => '捡漏必看'],
            ['key' => 'best', 'value' => '品质豪宅'],
        ],
        'order'           => [
            ['key' => 1, 'value' => '默认排序'],
            ['key' => 2, 'value' => '按价格从高到低'],
            ['key' => 3, 'value' => '按价格从低到高'],
            ['key' => 4, 'value' => '按折扣从高到低'],
            ['key' => 5, 'value' => '按折扣从低到高'],
            ['key' => 6, 'value' => '开拍时间由远及近'],
            ['key' => 7, 'value' => '开拍时间由近及远'],
            ['key' => 8, 'value' => '最近发布'],
        ],
        'lifts'           => [
            ['key' => 1, 'value' => '电梯'],
            ['key' => 2, 'value' => '楼梯'],
            ['key' => 3, 'value' => '未知'],
        ],
        'bedroom'         => [
            ['key' => 1, 'value' => '一居室'],
            ['key' => 2, 'value' => '二居室'],
            ['key' => 3, 'value' => '三居室'],
            ['key' => 4, 'value' => '四居室'],
            ['key' => 5, 'value' => '五居室以上'],
        ],
        //        'bulu'            => [
        //            ['key' => 1, 'value' => '未补录'],
        //            ['key' => 2, 'value' => '已补录']
        //        ],
        'source_type'     => [
            ['key' => 1, 'value' => '京东网'],
            ['key' => 2, 'value' => '阿里网'],
            ['key' => 3, 'value' => '人民法院诉讼资产网'],
            ['key' => 4, 'value' => '中国拍卖行业协会网'],
            ['key' => 5, 'value' => '公拍网'],
            ['key' => 6, 'value' => '工行融e购'],
            ['key' => 7, 'value' => '北京产权交易所'],
        ],
        'living_type'     => [
            ['key' => 1, 'value' => '未知'],
            ['key' => 2, 'value' => '租赁'],
            ['key' => 3, 'value' => '空置'],
            ['key' => 4, 'value' => '有居住']
        ],
        'renovation_type' => [
            ['key' => 0, 'value' => '未知'],
            ['key' => 1, 'value' => '清水'],
            ['key' => 2, 'value' => '简装'],
            ['key' => 3, 'value' => '中装'],
            ['key' => 4, 'value' => '精装'],
        ],
        'property_type'   => [
            ['key' => 1, 'value' => '未知'],
            ['key' => 2, 'value' => '产权房'],
            ['key' => 3, 'value' => '合同房'],
            ['key' => 4, 'value' => '房改房'],
            ['key' => 5, 'value' => '安置房'],
            ['key' => 6, 'value' => '福利房'],
            ['key' => 7, 'value' => '廉租房'],
        ],
        'house_cate'      => [
            ['key' => 0, 'value' => '普通'],
            ['key' => 1, 'value' => '跃层'],
            ['key' => 2, 'value' => '别墅'],
        ]
    ];

    //楼梯
    const ELEVVATORS = [
        0 => '- -',
        1 => '电梯',
        2 => '楼梯',
        3 => '未知',
    ];

    //拍卖阶段映射
    const A_TYPES = [
        0 => '未知',
        1 => '一拍',
        2 => '二拍',
        3 => '三拍',
        4 => '变卖',
        5 => '重新拍卖',
        6 => '破产',
    ];

    //拍卖状态映射
    const A_STATUSS = [
        0 => '未知',
        1 => '待开始',
        2 => '进行中',
        3 => '已成交',
        4 => '已流拍',
        5 => '已撤回',
        6 => '已终',
        7 => '已暂缓',
    ];

    //户型映射
    const BED_ROOM_COMFIG = [
        0 => '',
        1 => '一居室',
        2 => '二居室',
        3 => '三居室',
        4 => '四居室',
    ];

    //映射装修
    const RENOVATION_TYPE = [
        0 => '- -',
        1 => '清水',
        2 => '简装',
        3 => '中装',
        4 => '精装',
    ];

    /**
     * 房源列表
     * @param $params
     * @param $data_
     * @param $page_
     * @param $pageNum_
     * @param $count_
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getList($params, &$data, &$page, &$pageNum, &$count) {
        $column = array_merge(self::$h_column, self::$ha_column, self::$hv_column, self::$ht_column);
        list($map, $whereRaw, $order) = static::listWhere($params);
        self::getDatas($map, $whereRaw, $column, $order, (int)$params['page'], $params['long'], $data, $page, $pageNum, $count);
    }

    /**
     * 列表查询条件
     * @param $params
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    protected static function listWhere($params) {
        $map = [];
        $map[] = ['h.deleted', '=', 0];
        $whereRaw = '1';
        //全部房源
        if ($params['type'] == 1) {
            $map[] = ['h.is_shelf', '=', 1];
            $map[] = ['h.is_update', '=', 1];
        }

        //城市
        $city_code = $params['city_code'];
        $region_data = Db::name('region')->where('city_code', $city_code)->find();
        $codes = $region_data['p_codes'] . $city_code . ',';
        $map[] = ['h.city_codes', 'like', trim($codes) . '%'];

        //时间
        if (!empty($params['start_time']) && !empty($params['end_time'])) {
            $map[] = ['ha.start_time', '>=', strtotime(date('Y-m-d', strtotime($params['start_time'])) . '00:00:01')];
            $map[] = ['ha.start_time', '<=', strtotime(date('Y-m-d', strtotime($params['end_time'])) . '23:59:59')];
        }

        //区域
        if (!empty($params['area'])) {
            $map[] = ['h.city_code', '=', $params['area']];
        }

        //面积
        if (!empty($params['build_area'])) {
            $map[] = createSectionQuery1($params['build_area'], 'h.build_area');
        }

        //起拍价
        if (!empty($params['initial_price'])) {
            $map[] = createSectionQuery1($params['initial_price'], 'ha.initial_price');
        }

        //阶段
        if (!empty($params['a_type'])) {
            $map[] = ['ha.type', 'in', $params['a_type']];
        }

        //拍卖状态
        if (!empty($params['a_status'])) {
            $map[] = ['ha.auction_status', 'in', $params['a_status']];
        }

        //户型
        if (!empty($params['bedroom'])) {
            $bedroom = explode(',', $params['bedroom']);
            if (isset($bedroom[1])) {
                if (in_array(5, $bedroom)) {
                    $bedmap[0] = 'h.bedroom';
                    $bedmap[1][] = ['>=', 5];
                    $bedmap[1][] = ['in', $bedroom];
                    $bedmap[2] = 'or';
                } else {
                    $bedmap[] = ['h.bedroom', 'in', $bedroom];
                }
                $map[] = $bedmap;
            } else {
                if ($params['bedroom'] < 5) {
                    $map[] = ['h.bedroom', 'in', $bedroom];
                } else {
                    $map[] = ['h.bedroom', '>=', $params['bedroom']];
                }
            }
        }

        //折扣比
        if (!empty($params['discount'])) {
            $whereRaw .= ' and ';
            $whereRaw .= createSectionDiscount($params['discount'], 'ha.discount');
        }

        //楼层
        if (!empty($params['house_floor'])) {
            $map[] = createSectionQuery1($params['house_floor'], 'h.floor');
        }

        //来源
        if (!empty($params['source_type'])) {
            $map[] = ['h.source_type', 'in', $params['source_type']];
        }

        //房源类型
        if (!empty($params['house_type'])) {
            $map[] = ['h.house_type_id', 'in', $params['house_type']];
        }

        // 其他
        if (!empty($params['other'])) {
            $villa_config = Db::name('villa')->where("city_code", $city_code)->field('price')->find();
            if (!empty($villa_config['pirce'])) {
                $_best = ['initial_price', '>=', $villa_config['pirce']];
            } else {
                $_best = ['initial_price', '>=', 500000000];
            }

            $otherMap = [
                'hot'     => [
                    ['h.important', '=', 1],//重点
                    ['ha.auction_status', '=', 1]//待开始
                ],
                'today'   => [
                    ['ha.start_time', '>=', strtotime(date('Y-m-d', time()))],
                    ['ha.start_time', '<', strtotime(date('Y-m-d', strtotime('5 day')))],
                    ['ha.auction_status', '=', 1],
                ],
                'pick_up' => [
                    ['ha.type', 'in', self::$ha_type],//非一拍
                    ['ha.auction_status', 'in', self::$ha_astatus]//待开始，进行中
                ],
                'best'    => [
                    ['ha.auction_status', 'in', self::$ha_astatus],//待开始，进行中
                    $_best
                ]
            ];

            $other = explode(',', $params['other']);
            foreach ($other as $j => $k) {
                $map[] = $otherMap[$k];
            }
        }

        //房源标题或小区标题
        if (!empty($params['h_title'])) {
            $map[] = ['h.title|hv.name', 'like', '%' . trim($params['h_title']) . '%'];
        }

        //排序
        $order = house_order($params['order']??'');


        !isset($params['page']) ? $params['page'] = 1 : $params['page'];
        !isset($params['long']) ? $params['long'] = 12 : $params['long'];

        $map[] = ['ha.type', 'in', [0, 1, 2, 3, 4, 5, 6]];//避免出现没有拍卖数据的房源
        return [$map, $whereRaw, $order];
    }

    /**
     * 筛选多条数据
     * @param $map
     * @param $column
     * @param $order
     * @param $start
     * @param $long
     * @param $data
     * @param $page
     * @param $pageNum
     * @param $count
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getDatas($map, $whereRaw, $column, $order, $start, $long, &$data, &$page, &$pageNum, &$count) {
        $data = db('house')
            ->alias('h')
            ->leftJoin('house_village hv', 'hv.id=h.village_id')
            ->leftJoin('house_auction ha', 'ha.id=h.auction_id')
            ->leftJoin('house_type ht', 'ht.id=h.house_type_id')
            ->where($map);
        if ($whereRaw) {
            $data->whereRaw($whereRaw);
        }
        $data = $data->order($order)
            ->page($start, $long)
            ->field($column)
            ->fieldRaw("(" . Db::name('house_err')->where('status', 0)->where('house_id', 'h.id')->fetchSql(true)->count() . ") as err_num")
            ->select();

        $count = db('house')
            ->alias('h')
            ->leftJoin('house_village hv', 'hv.id=h.village_id')
            ->leftJoin('house_auction ha', 'ha.id=h.auction_id')
            ->leftJoin('house_type ht', 'ht.id=h.house_type_id')
            ->where($map);
        if ($whereRaw) {
            $count->whereRaw($whereRaw);
        }
        $count = $count->count();
        foreach ($data as $k => $v) {
            $data[$k]['initial_price'] = deal_data_info($v['initial_price'], 6);
            $data[$k]['type'] = self::A_TYPES[$v['type']];
            $data[$k]['auction_status'] = self::A_STATUSS[$v['auction_status']];
            $data[$k]['start_time'] = empty($data[$k]['start_time']) ? '' : date('Y/m/d H:i:s', $v['start_time']);
            $data[$k]['end_time'] = empty($data[$k]['end_time']) ? '' : date('Y/m/d H:i:s', $v['end_time']);
            $data[$k]['hv_name'] = empty($data[$k]['hv_name']) ? '' : $data[$k]['hv_name'];
            $data[$k]['build_area'] = empty($data[$k]['build_area']) ? '' : deal_area_list($data[$k]['build_area'], 4) . '㎡';
            if ($data[$k]['discount'] <= 1 && $data[$k]['discount'] != -1) {
                $data[$k]['discount'] = '低于1折';
            } elseif ($data[$k]['discount'] == -1) {
                $data[$k]['discount'] = '- -';
            } else {
                $data[$k]['discount'] = $v['discount'];
            }
            if ($data[$k]['bedroom'] >= 5) {
                $data[$k]['bedroom'] = '五居室以上';
            } else {
                $data[$k]['bedroom'] = self::BED_ROOM_COMFIG[$data[$k]['bedroom']];
            }
            if ($data[$k]['floor']) {
                $data[$k]['floor'] = $data[$k]['floor'] . '楼';
            } else {
                $data[$k]['floor'] = '';
            }

            $sourceList = House::$whereList['source_type'];
            foreach ($sourceList as $arr => $item)
                $sourceList[$arr]['key'] == $data[$k]['source_type'] && $data[$k]['source_type'] = $sourceList[$arr]['value'];

            $data[$k]['ht_name'] = $data[$k]['ht_name'] ?? '';
        }
        $page = $start;
        if ($long > 0) {
            $pageNum = ceil($count / $long);
        } else {
            $pageNum = 1;
        }
    }

    /**
     * 房源详情
     * @param $params
     * @return array|PDOStatement|string|Model|null
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function houseInfo($params) {
        $data = db("house")
            ->alias('h')
            ->leftJoin('house_village hv', 'hv.id=h.village_id')
            ->leftJoin('house_auction ha', 'ha.id=h.auction_id')
            ->leftJoin('house_type ht', 'ht.id=h.house_type_id')
            ->field('h.title,ht.name as htname,h.address,h.city_codes,h.build_area,h.bedroom,h.floor,h.build_time,h.lifts,ha.discount,h.cover,ha.id as action_id,h.business_area_id,h.village_id,h.pic,x(hv.location) as lng,Y(hv.location) as lat,h.livingroom,h.renovation_type,h.province,h.city,h.area,hv.name as hv_name,IF(ha.type=4,ha.pay_selloff,ha.initial_price) as initial_price,ha.consult_price,ha.market_price,h.house_cate,h.anjuke_url,h.anjuke_price,h.living_type,h.warrant,h.property_type')
            ->where('h.deleted', 0)
            ->find($params['id']);
        if (empty($data)) {
            Err::add('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        $address_detail = $data['province'] . '-' . $data['city'] . '-' . $data['area'];
        $rank = substr($data['city_codes'], 1, strlen($data['city_codes']) - 2);
        unset($data['province']);
        unset($data['city']);
        unset($data['area']);

        if (!empty($data['business_area_id'])) {
            $BusinessArea = BusinessArea::getOne([['id', '=', $data['business_area_id']]], 'name,city_id');
            $address_detail .= '-' . $BusinessArea['name'];
            $rank .= ',' . $data['business_area_id'];
        }
        if (!empty($data['village_id'])) {
            $village = HouseVillage::getOne([['id', '=', $data['village_id']]], 'name,city_code');
            $address_detail .= '-' . $village['name'];
            $rank .= ',' . $data['village_id'];
        }
        $data['ranks'] = $rank;
        $data['address_detail'] = $address_detail;
        $data['cover'] = empty($data['cover']) ? config('global.empty_house_img') : $data['cover'];
        unset($data['city_codes']);
        unset($data['district']);
        unset($data['business_area_id']);
        unset($data['village_id']);
        $data['pic'] = empty($data['pic']) ? [] : explode(',', $data['pic']);
        $data['lifts'] = self::ELEVVATORS[$data['lifts']];
        $data['renovation_type'] = self::RENOVATION_TYPE[$data['renovation_type']];
        $data['build_area'] = empty($data['build_area']) ? '- -' : deal_data_info($data['build_area'], 4) . '㎡';
        $data['build_time'] = empty($data['build_time']) ? '- -' : $data['build_time'];
//        $data['discount'] = $data['discount'] <= 1 ? '低于1折' : (string)$data['discount'] . '折';
        if ($data['discount'] <= 1 && $data['discount'] != -1) {
            $data['discount'] = '低于1折';
        } elseif ($data['discount'] == -1) {
            $data['discount'] = '- -';
        } else {
            $data['discount'] = (string)$data['discount'] . '折';
        }
        deal_money_info($data['initial_price'], $initial_price, $initial_price_unit);
        $data['initial_price'] = $initial_price . $initial_price_unit;
        deal_money_info($data['consult_price'], $consult_price, $consult_price_unit);
        $data['consult_price'] = $consult_price . $consult_price_unit;
        deal_money_info($data['market_price'], $market_price, $market_price_unit);
        $data['market_price'] = $market_price . $market_price_unit;
        $data['room'] = empty($data['bedroom']) || empty($data['livingroom']) ? '- -' : $data['bedroom'] . '室' . $data['livingroom'] . '厅';
        unset($data['livingroom']);
        unset($data['bedroom']);
        $data['floor'] = empty($data['floor']) ? '- -' : $data['floor'] . '楼';
        $data['hv_name'] = empty($data['hv_name']) ? '' : $data['hv_name'];
        $data['lat'] = empty($data['lat']) ? '' : $data['lat'];
        $data['lng'] = empty($data['lng']) ? '' : $data['lng'];
        if ($data['house_cate'] = 0) $data['house_cate'] = '普通';
        if ($data['house_cate'] = 1) $data['house_cate'] = '跃层';
        if ($data['house_cate'] = 2) $data['house_cate'] = '别墅';
        $data['anjuke_price'] = $data['anjuke_price'] / 100;
        if ($data['living_type'] = 1) $data['living_type'] = '未知';
        if ($data['living_type'] = 2) $data['living_type'] = '租赁';
        if ($data['living_type'] = 3) $data['living_type'] = '空置';
        if ($data['living_type'] = 4) $data['living_type'] = '有居住';
        if ($data['property_type'] = 1) $data['property_type'] = '未知';
        if ($data['property_type'] = 2) $data['property_type'] = '产权房';
        if ($data['property_type'] = 3) $data['property_type'] = '合同房';
        if ($data['property_type'] = 4) $data['property_type'] = '房改房';
        if ($data['property_type'] = 5) $data['property_type'] = '安置房';
        if ($data['property_type'] = 6) $data['property_type'] = '福利房';
        if ($data['property_type'] = 7) $data['property_type'] = '廉租房';
        return $data;
    }

    /**
     * 通过区域查询
     * @param $area_code
     * @return array
     */
    public static function houseByArea($params) {
        $map[] = ['h.city_code', 'in', $params['area_code']];
        $map[] = ['h.is_shelf', '=', 1];
        $map[] = ['h.deleted', '=', 0];
        $map[] = ['h.is_update', '=', 1];
        $map[] = ['ha.auction_status', 'in', [1, 2]];
        $orderRaw = $params['area_code'];
        return self::previewHouse($map, $orderRaw);
    }

    /**
     * 1,2版预览房源样式
     * @param $map
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function previewHouse($map, $orderRaw) {
        $data = db("house")
            ->alias('h')
            ->leftJoin('house_village hv', 'hv.id = h.village_id')
            ->leftJoin('house_auction ha', 'h.auction_id = ha.id')
            ->where($map)
            ->orderRaw("field(h.city_code,$orderRaw)")
            ->field('h.id,hv.name as hv_name,h.title,h.build_time,h.floor,h.renovation_type,h.livingroom,h.bedroom,h.build_area,ha.initial_price,ha.market_price,ha.discount,ha.type,ha.start_time,ha.end_time,h.area')
            ->select();
        if ($data) {
            foreach ($data as &$val) {
                if ($val['discount'] <= 1 && $val['discount'] != -1) {
                    $discount = '低于1折';
                } elseif ($val['discount'] == -1) {
                    $discount = '- -';
                } else {
                    $discount = (string)$val['discount'] . '折';
                }
                $arr = [
                    'hv_name'         => empty($val['hv_name']) ? '' : $val['hv_name'],
                    'title'           => $val['title'],
                    'build_time'      => $val['build_time'],
                    'floor'           => $val['floor'],
                    'renovation_type' => self::RENOVATION_TYPE[$val['renovation_type']],
                    'livingroom'      => $val['livingroom'],
                    'bedroom'         => $val['bedroom'],
                    'build_area'      => deal_data_info($val['build_area'], 4),
                    'initial_price'   => deal_data_info($val['initial_price'], 6),
                    'market_price'    => deal_data_info($val['market_price'], 6),
                    'discount'        => $discount,
                    'type'            => self::A_TYPES[$val['type']],
                    'start_time'      => date('m月-d日', $val['start_time']),
                    'end_time'        => date('m月-d日', $val['end_time']),
                ];
                $tree[$val['area']]['area'] = $val['area'];
                $tree[$val['area']]['children'][] = $arr;
                for ($index1 = 0; $index1 < count($tree[$val['area']]['children']); $index1++) {
                    $tree[$val['area']]['children'][$index1]['id'] = $index1 + 1;
                }
            }
            $res = array_values($tree);
        } else {
            return false;
        }
        return $res;
    }

    /**
     * 通过城市
     * @param $area_code
     * @return array
     */
    public static function houseByCity($params) {
        $region_data = Db::name('region')->where('city_code', $params['city_code'])->find();
        $codes = $region_data['p_codes'] . $params['city_code'] . ',';
        $map[] = ['h.city_codes', 'like', trim($codes) . '%'];
        $map[] = ['h.is_shelf', '=', 1];
        $map[] = ['h.deleted', '=', 0];
        $map[] = ['h.is_update', '=', 1];
        $map[] = ['h.house_cate', 'in', [1, 2]];
        $map[] = ['ha.auction_status', 'in', [1, 2]];
        return self::previewHouse2($map);
    }

    /**
     * 3版预览房源样式
     * @param $map
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function previewHouse2($map) {
        $city_code = db('region')->order('weights', 'asc')->column('city_code');
        $orderRaw = join(',', $city_code);
        $res = db("house")
            ->alias('h')
            ->leftJoin('house_village hv', 'hv.id = h.village_id')
            ->leftJoin('house_auction ha', 'h.auction_id = ha.id')
            ->where($map)
            ->field('h.id,hv.name as hv_name,h.build_time,h.floor,h.renovation_type,h.livingroom,h.bedroom,h.build_area,ha.initial_price,ha.market_price,ha.discount,ha.start_time,ha.end_time,h.area,h.house_cate')
            ->orderRaw("field(h.city_code,$orderRaw)")
            ->order('h.house_cate', 'desc')
            ->select();
        if ($res) {
            foreach ($res as &$val) {
                if ($val['discount'] <= 1 && $val['discount'] != -1) {
                    $discount = '低于1折';
                } elseif ($val['discount'] == -1) {
                    $discount = '- -';
                } else {
                    $discount = (string)$val['discount'] . '折';
                }
                $arr = [
                    'hv_name'         => empty($val['hv_name']) ? '' : $val['hv_name'],
                    'build_time'      => $val['build_time'],
                    'floor'           => $val['floor'],
                    'renovation_type' => self::RENOVATION_TYPE[$val['renovation_type']],
                    'livingroom'      => $val['livingroom'],
                    'bedroom'         => $val['bedroom'],
                    'build_area'      => deal_data_info($val['build_area'], 4),
                    'initial_price'   => deal_data_info($val['initial_price'], 6),
                    'market_price'    => deal_data_info($val['market_price'], 6),
                    'discount'        => $discount,
                    'start_time'      => date('m月-d日', $val['start_time']),
                    'end_time'        => date('m月-d日', $val['end_time']),
                    'area'            => $val['area'],
                    'house_cate'      => $val['house_cate'],
                ];
                $tree[$val['house_cate']]['house'] = $val['house_cate'];
                $tree[$val['house_cate']]['children'][] = $arr;
                for ($index1 = 0; $index1 < count($tree[$val['house_cate']]['children']); $index1++) {
                    $tree[$val['house_cate']]['children'][$index1]['id'] = $index1 + 1;
                }
            }
            $res = array_values($tree);
        } else {
            return false;
        }
        return $res;
    }

    /**
     * 区间数据
     * @param $city_code
     * @return mixed
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function allIntervel($city_code) {
        $interval_config = Db::name('interval_config')
            ->order('min', 'asc')
            ->whereOr('city_code', 0)
            ->whereOr('city_code', $city_code)
            ->field('type,name as value,min,max')
            ->select();
        $i = 0;
        do {
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE1) {
                $all_intervel['price_interval'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE2) {
                $all_intervel['build_area_interval'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE3) {
                $all_intervel['discount_interval'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE4) {
                $all_intervel['house_age'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE5) {
                $all_intervel['house_floor'][] = self::deal($interval_config[$i]);
            }
            $i++;
        } while ($i < count($interval_config));
        return $all_intervel;
    }

    /**
     * 区间处理
     * @param $arr
     * @return mixed
     */
    public static function deal($arr) {
        $arr['key'] = $arr['min'] . '-' . $arr['max'];
        unset($arr['min']);
        unset($arr['max']);
        unset($arr['type']);
        return $arr;
    }

}
